import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:xingsu/page/video/model/Video_model.dart';
import 'package:xingsu/page/video/video_horizontal_page.dart';
import 'package:xingsu/util/image_util.dart';

import '../../res/colors.dart';

class VideoStatusPage extends StatefulWidget {
  final int index;

  const VideoStatusPage({Key? key, required this.index}) : super(key: key);

  @override
  State<VideoStatusPage> createState() => _VideoStatusPageState();
}

class _VideoStatusPageState extends State<VideoStatusPage>
    with AutomaticKeepAliveClientMixin<VideoStatusPage> {
  List _list = [];

  @override
  void initState() {
    super.initState();

    loadData();
  }

  void loadData() async {
    String jsonData = await rootBundle.loadString('assets/json/status.json');
    List allDatas = json.decode(jsonData);

    int index = widget.index;
    if (index == 0) {
      _list.assignAll(allDatas.map((e) => VideoModel.fromJson(e)).toList());
    } else if (index == 1) {
      for (Map map in allDatas) {
        int status = map["status"] ?? 0;
        if (status == 0) {
          VideoModel model = VideoModel.fromJson(map);
          _list.add(model);
        }
      }
    } else if (index == 2) {
      for (Map map in allDatas) {
        int status = map["status"] ?? 0;
        if (status == 1) {
          VideoModel model = VideoModel.fromJson(map);
          _list.add(model);
        }
      }
    }

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return _list.isEmpty
        ? Center(
            child: Text(
              "列表为空",
              style: TextStyle(fontSize: 20),
            ),
          )
        : ListView.builder(
            itemCount: _list.length,
            itemBuilder: (context, index) => _itemWidget(index));
  }

  Widget _itemWidget(int index) {
    VideoModel model = _list[index];
    return InkWell(
      onTap: () {
        Get.to(() => VideoHorizontalPage(model));
      },
      child: Container(
        margin: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
        decoration: BoxDecoration(
            color:
                context.isDarkMode ? Colours.dark_material_bg : Colours.bg_gray,
            borderRadius: BorderRadius.circular(10)),
        height: 100,
        padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
        alignment: Alignment.centerLeft,
        child: Row(
          children: [
            loadNetworkImage(model.gameCoverImg ?? "", width: 100),
            const SizedBox(
              width: 10,
            ),
            Expanded(
                child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(model.videoTitle ?? ""),
                const SizedBox(
                  height: 10,
                ),
                Row(
                  children: [
                    Text(
                      model.date ?? "",
                      style: const TextStyle(color: Colors.grey, fontSize: 13),
                    ),
                    const Spacer(),
                    const Text(
                      "播放量: 0",
                      style: TextStyle(color: Colors.grey, fontSize: 13),
                    ),
                  ],
                )
              ],
            ))
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
